//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   FACT_AUDIT
// AUTHOR           :   Ray Guo 
// DATE CREATE      :   2007/09/10
// DESCRIPTION      :   a BIZ LOGIC file for the function, FACT_AUDIT
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/09/10   Ray Guo         1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.compliance.factAudit.bizLogic;

import java.sql.Connection;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.attachment.beans.AttachmentVo;
import com.erp.app.compliance.factAudit.dao.FactAuditCommonDao;
import com.erp.app.compliance.factAudit.vo.FactAuditHdrVo;
import com.erp.app.compliance.factAudit.bizLogic.FactAuditCommonUtil;
import com.erp.app.compliance.factAudit.vo.FactAuditAttachVo;
import com.erp.app.compliance.factAudit.vo.FactAuditHdrVo;

import com.erp.framework.model.bizLogic.SimpleSearchMaintStyleBizLogic;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.types.CustomBigInteger;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.DbObjectUtil;
import com.erp.framework.util.logger.Logger;

/**
* <p>A <strong>BizLogic</strong> provides the logical flow and control
* of the business model at different transaction time.
*
* @author          Ray Guo 
* @version 1.0     2007/09/10
* @since           FrameWork 1.0
*
*/
public class FactAuditBizLogic extends SimpleSearchMaintStyleBizLogic {

	private FactAuditCommonUtil util = new FactAuditCommonUtil();
	private FactAuditCommonDao dao = new FactAuditCommonDao();

	/**
	* Generates a unique document number.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void genDocNo(SimpleBaseVo model, Connection conn) throws Exception {

		//TODO:
		//Uncomment the following code and modify accordingly

		FactAuditHdrVo vo = (FactAuditHdrVo) model;
		vo.setAuditNo(util.generateDocumentNo(model, conn));
	}

	/**
	* Provides logics to insert business object before creating the object.
	* Note that this function has NO user transaction control.
	*
	* @param      model   the incoming business model (VO)
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsBeforeCreate(SimpleBaseVo model) throws Exception {
		Logger.debug(this, "in bizLogicsBeforeCreate()");
	}

	/**
	* Provides logics to insert business object before creating the object.
	* Note that this function has user transaction control.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsBeforeCreateModel(SimpleBaseVo model, Connection conn) throws Exception {
		handleAttachmentChilds(model, conn, false);
		Logger.debug(this, "in bizLogicsBeforeCreateModel()");
		//FactAuditCommonUtil.setPkForNonStandardChildren(model);
	}

	/**
	* Provides logics to insert business object after creating the object.
	* Note that this function has user transaction control.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsAfterCreateModel(SimpleBaseVo model, Connection conn) throws Exception {

		Logger.debug(this, "in bizLogicsAfterCreateModel()");
	}
	
	/**
	* Provides logics to insert business object after creating the object.
	* Note that this function has NO user transaction control.
	*
	* @param      model   the incoming business model (VO)
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsAfterCreate(SimpleBaseVo model) throws Exception {

		Logger.debug(this, "in bizLogicsAfterCreate()");
	}

	/**
	* Provides logics to update business object before updating the object.
	* Note that this function has NO user transaction control.
	*
	* @param      model   the incoming business model (VO)
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsBeforeUpdate(SimpleBaseVo model) throws Exception {

		Logger.debug(this, "in bizLogicsBeforeUpdate()");
	}
	
	/**
	* Provides logics to update business object before updating the object.
	* Note that this function has user transaction control.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsBeforeUpdateModel(SimpleBaseVo model, Connection conn) throws Exception {
		handleAttachmentChilds(model, conn, false);
		Logger.debug(this, "in bizLogicsBeforeUpdateModel()");
	}

	/**
	* Provides logics to update business object after updating the object.
	* Note that this function has user transaction control.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsAfterUpdateModel(SimpleBaseVo model, Connection conn) throws Exception {

		Logger.debug(this, "in bizLogicsAfterUpdateModel()");
	}

	/**
	* Provides logics to update business object after updating the object.
	* Note that this function has NO user transaction control.
	*
	* @param      model   the incoming business model (VO)
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsAfterUpdate(SimpleBaseVo model) throws Exception {

		Logger.debug(this, "in bizLogicsAfterUpdate()");
	}
	
	/**
	* Provides logics to remove business object before removing the object.
	* Note that this function has NO user transaction control.
	*
	* @param      model   the incoming business model (VO)
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsBeforeRemove(SimpleBaseVo model) throws Exception {

		Logger.debug(this, "in bizLogicsBeforeRemove()");
	}
	
	/**
	* Provides logics to remove business object before removing the object.
	* Note that this function has user transaction control.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsBeforeRemoveModel(SimpleBaseVo model, Connection conn) throws Exception {
		handleAttachmentChilds(model, conn, true);
		Logger.debug(this, "in bizLogicsBeforeRemoveModel()");
		
	}

	/**
	* Provides logics to remove business object after removing the object.
	* Note that this function has user transaction control.
	*
	* @param      model   the incoming business model (VO)
	* @param      conn    the database connection
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsAfterRemoveModel(SimpleBaseVo model, Connection conn) throws Exception {

		Logger.debug(this, "in bizLogicsAfterRemoveModel()");
	}
	/**
	* Provides logics to remove business object after removing the object.
	* Note that this function has NO user transaction control.
	*
	* @param      model   the incoming business model (VO)
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public void bizLogicsAfterRemove(SimpleBaseVo model) throws Exception {

		Logger.debug(this, "in bizLogicsAfterRemove()");
	}
	
	public void handleAttachmentChilds(SimpleBaseVo model, Connection conn, boolean deleteFlag) throws Exception {
		FactAuditHdrVo hdrVo = (FactAuditHdrVo)model;
		ChildList attachmentList = hdrVo.getChilds(FactAuditAttachVo.class);
		for(int i=0;attachmentList!=null && i<attachmentList.size(); i++){
			FactAuditAttachVo vo = (FactAuditAttachVo) attachmentList.get(i);
			AttachmentVo attachmentVo = new AttachmentVo();
			attachmentVo.setCompanyCode(vo.getCompanyCode());
			attachmentVo.setAttachmentAttachmentLineNo(vo.getAttachAttachLineNo());
			attachmentVo.setAttachmentDescription(vo.getAttachAttachDesc());
			attachmentVo.setAttachmentContentType(vo.getAttachContentType());
			attachmentVo.setAttachmentFileName(vo.getAttachFileName());
			attachmentVo.setAttachment(vo.getAttachAttachment());
			attachmentVo.setCreateUser(vo.getCreateUser());
			attachmentVo.setUpdateUser(vo.getUpdateUser());
			if(deleteFlag){
				if(!CommonUtil.isEmpty(attachmentVo.getAttachmentAttachmentLineNo())){
					DbObjectUtil.deleteDbAttachment(attachmentVo, conn);
					vo.setAttachAttachLineNo(null);
				}
			}else if(CommonUtil.nullToString(vo.getAttachActionType()).equals(ProjectConstants.ACTION_TYPE_INSERT)){
				CustomBigInteger attachmentLineNo = new CustomBigInteger(DbObjectUtil.getNextAttachmentLineNo(attachmentVo, conn));
				attachmentVo.setAttachmentAttachmentLineNo(attachmentLineNo);
				vo.setAttachAttachLineNo(attachmentLineNo);
				DbObjectUtil.insertDbAttachment(attachmentVo, conn);
			}else if(CommonUtil.nullToString(vo.getAttachActionType()).equals(ProjectConstants.ACTION_TYPE_UPDATE)){
				DbObjectUtil.updateDbAttachment(attachmentVo, conn);
			}else if(CommonUtil.nullToString(vo.getAttachActionType()).equals(ProjectConstants.ACTION_TYPE_DELETE)){
				DbObjectUtil.deleteDbAttachment(attachmentVo, conn);
				vo.setAttachAttachLineNo(null);
			}else if(CommonUtil.isEmpty(vo.getAttachActionType())){
				if(!CommonUtil.nullToString(vo.getAttachOldDesc()).equals(CommonUtil.nullToString(vo.getAttachAttachDesc()))){
					DbObjectUtil.updateDbAttachment(attachmentVo, conn, false);
				}
			}
		}
	}
	
}
